﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GNSystem.Models;
using WebMatrix.WebData;

namespace GNSystem.Controllers
{
    [GNSystem.Filters.InitializeSimpleMembership]
    public class ThreadController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /Thread/Details/5

        public ActionResult Details(int id = 0)
        {
            Thread thread = db.Threads.Find(id);
            if (thread == null)
            {
                return HttpNotFound();
            }
            ViewBag.Context = db;
            ViewBag.UID = WebSecurity.CurrentUserId;
            return View(thread);
        }

        //
        // GET: /Thread/Create

        public ActionResult Create(int fid)
        {
            if (!db.Forums.ToList().Exists(f => f.ID == fid))
                return RedirectToAction("Index", "Forums");
            ViewBag.FID = fid;
            return View();
        }

        //
        // POST: /Thread/Create

        [HttpPost]
        public ActionResult Create(Thread thread)
        {
            if (ModelState.IsValid)
            {
                thread.CreationTime = DateTime.Now;
                thread.UserID = WebSecurity.CurrentUserId;
                db.Threads.Add(thread);
                db.SaveChanges();
                return RedirectToAction("Details", thread.ID);
            }

            return View(thread);
        }

        //
        // GET: /Thread/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Thread thread = db.Threads.Find(id);
            if (thread == null)
            {
                return HttpNotFound();
            }
            return View(thread);
        }

        //
        // POST: /Thread/Edit/5

        [HttpPost]
        public ActionResult Edit(Thread thread)
        {
            if (ModelState.IsValid)
            {
                if (thread.CreationTime < DateTime.Parse("1/1/1753"))
                    thread.CreationTime = DateTime.Now;
                db.Entry(thread).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(thread);
        }

        //
        // GET: /Thread/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Thread thread = db.Threads.Find(id);
            if (thread == null)
            {
                return HttpNotFound();
            }
            return View(thread);
        }

        //
        // POST: /Thread/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Thread thread = db.Threads.Find(id);
            db.Threads.Remove(thread);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}